/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.customerManagement.controller;

import java.util.regex.Pattern;

/**
 *
 * @author illabefat
 */
public class ValuesValidator {
    
    private static Pattern ID_PATTERN = Pattern.compile("[1-9][0-9]*");
    private static Pattern NAME_PATTERN = Pattern.compile("^[a-z|A-Z|а-я|А-Я]+\\s+[a-z|A-Z|а-я|А-Я]+$");
    private static Pattern LOCATION_PATTERN = Pattern.compile("^([a-z|A-Z|а-я|А-Я]*[\\.|\\s]?\\s?[a-z|A-Z|а-я|А-Я]*[,|\\s]\\s*){1,2}[д|Д]?\\.?\\s*\\d+[-|\\s|,]\\s*(кв|КВ)?\\.?\\s*\\d+$"); //Город-необязательно, улица, дом 88 или д. 88 или д 88 или 88
     
    
    public static Long getCorrectId(String str) throws IllegalArgumentException
    {
        if (str != null && ID_PATTERN.matcher(str).matches())
        {
            return Long.valueOf(str);
        }
        else
        {
            throw new IllegalArgumentException("Id must be a positive number!");
        }
    }
    
    public static String getCorrectCustomerName(String str) throws IllegalArgumentException
    {
        if (str != null && NAME_PATTERN.matcher(str).matches())
        {
            return str;
        }
        else
        {
            throw new IllegalArgumentException("Name must contain only 2 words");
        }
    }
    
    public static String getCorrectCustomerLocationAddress(String str) throws IllegalArgumentException
    {
        if (str != null && LOCATION_PATTERN.matcher(str).matches())
        {
            return str;
        }
        else
        {
            throw new IllegalArgumentException("Incorrect address format");
        }
    }
    
}
